package com.bangbang.controller;

import com.bangbang.configresult.Result;
import com.bangbang.configresult.ResultEnum;
import com.bangbang.pojo.TbCourse;
import com.bangbang.service.ITbCourseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/ShoppimgCart")
@Api("购物车详情接口")
public class ShoppimgCart {
//    注入课程商品表
    @Autowired
    private ITbCourseService iTbCourseService;


    @PostMapping
    @ApiOperation("加入购物车")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "couId",value = "课程ID", required = true,paramType = "query"),
    })                                                 //课程ID
    public Result save(@RequestParam(required = true) Long couId) {

        try {


            Long uid = 1L;
            //                根据课程ID查询出课程详情信息
            TbCourse course = iTbCourseService.selectById(couId);

            //            查询缓存中是否存在该教师的购物车                    放置当前用户ID
            List<TbCourse> courseList = iTbCourseService.selectRedis(uid);
            if (courseList.size() > 0) {//说明当前用户在缓存中有购物车信息 那么我们将当前课程信息追加到redis中
                //                一: 缓存当中有该购物车信息 那就把当前的课程添加到redis中
                courseList.add(course);
                iTbCourseService.saveRedis(uid, courseList);
            } else {//说明缓存中没有当前用户的购物车信息  那么将当前数据放置到reids中
                courseList.add(course);
                iTbCourseService.saveRedis(uid, courseList);
            }
            return Result.buildResult(ResultEnum.SUCCESS);
        } catch (Exception e) {
            return Result.buildResult(ResultEnum.ERROR);
        }
    }

    @GetMapping
    @ApiOperation("查询当前用户购物车")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "uId",value = "用户ID", required = true,paramType = "query"),
    })
    public Result selectGwc(@RequestParam(required = true,value = "uId")Long id){
        List<TbCourse> courseList = iTbCourseService.selectGwc(id);
        return Result.buildResult(ResultEnum.SUCCESS).setResult(courseList);
    }
}
